package cn.zjxiliu.lib_login.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import cn.zjxiliu.lib_core.widget.ClearEditText;
import cn.zjxiliu.lib_login.R;

/**
 * Created by jessie on 2018/11/26.
 */

public class PhoneInputNumView extends LinearLayout {

    private LinearLayout inputPhoneNumView;

    private ClearEditText inputPhoneNo;
    private View viewLine;

    private InputViewCallbackListener listener;


    public void setListener(InputViewCallbackListener listener) {
        this.listener = listener;
    }

    public PhoneInputNumView(Context context) {
        super(context);
    }

    public PhoneInputNumView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inputPhoneNumView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.input_phonenum_view, null);

        inputPhoneNo = (ClearEditText) inputPhoneNumView.findViewById(R.id.inputPhoneNo);
        inputPhoneNo.setPhoneMode();
        viewLine =  inputPhoneNumView.findViewById(R.id.viewLine);


        initListen();
        addView(inputPhoneNumView);
    }

    public PhoneInputNumView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initListen() {
        inputPhoneNo.setTextChangeListener(new ClearEditText.OnTextChangeListener() {
            @Override
            public void onFocusChange(boolean hasFocus) {
                if (hasFocus) {
                    viewLine.setBackgroundColor(getResources().getColor(R.color.c3F86FF));
                    if (listener != null) {
                        listener.onFocusChange(hasFocus);
                    }
                } else {
                    viewLine.setBackgroundColor(getResources().getColor(R.color.cDBDBDB));
                    if (listener != null) {
                        listener.onFocusChange(hasFocus);
                    }
                }
            }

            @Override
            public void afterTextChanged(String s) {
                if (TextUtils.isEmpty(s)) {
                    if (listener != null) {
                        listener.inputViewHasText(false);
                    }
                } else {

                    if (listener != null) {
                        if(getText().length() == 11){
                            listener.inputViewHasText(true);
                        }else{
                            listener.inputViewHasText(false);
                        }

                    }
                }
            }
        });
    }


    public String getText() {
        return inputPhoneNo.getText().toString().trim().replaceAll(" ", "");
    }


    public String getHideModelText(){
        String phoneNo = inputPhoneNo.getText().toString().trim().replaceAll(" ", "");
        if(phoneNo.length() == 11){
            String result = phoneNo.substring(0, 3) + "******" + phoneNo.substring(9);
            return result;
        }
        return "";
    }


    public PhoneInputNumView setHintText(String text) {
        inputPhoneNo.setHint(text);
        return this;
    }

    public PhoneInputNumView setInputType(int type) {
        inputPhoneNo.setInputType(type);
        return this;
    }

    public PhoneInputNumView isHideUnderline(boolean isHide) {
        if (isHide) {
            viewLine.setVisibility(GONE);
        } else {
            viewLine.setVisibility(VISIBLE);
        }
        return this;
    }

    public PhoneInputNumView setHintColor(int color) {
        inputPhoneNo.setHintTextColor(color);
        return this;
    }

    public PhoneInputNumView setTextSize(float size) {
        inputPhoneNo.setTextSize(size);
        return this;
    }

    public PhoneInputNumView setTextColor(int color) {
        inputPhoneNo.setTextColor(color);
        return this;
    }

    public PhoneInputNumView setMaxLength(int maxLength) {
        inputPhoneNo.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
        return this;
    }

    public PhoneInputNumView setDrawableLeft(Drawable drawableLeft) {
        inputPhoneNo.setDrawableLeft(drawableLeft);
        return this;
    }

}
